package com.imay.live.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.imay.live.R;


/**
 * Created by Administrator on 2017/04/17 0017.
 */

public class SearchLinearLayout extends LinearLayout {
    Paint paint;
    Path path;
    float left;

    public SearchLinearLayout(Context context) {
        super(context);
    }

    public SearchLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SearchLinearLayout);
            left = a.getDimension(R.styleable.SearchLinearLayout_moveleft, 0);
            a.recycle();
        }
    }

    public SearchLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SearchLinearLayout);
            left = a.getDimension(R.styleable.SearchLinearLayout_moveleft, 0);
            a.recycle();
        }
    }

    @RequiresApi(api = 21)
    public SearchLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (path == null) {
            path = new Path();
            paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(3);
            paint.setColor(Color.parseColor("#FFECECEC"));
        }
        path.reset();
        path.moveTo(left, canvas.getHeight());
        path.lineTo(canvas.getWidth(), canvas.getHeight());
        canvas.drawPath(path, paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int maxwidth = getMeasuredWidth();
        LinearLayout search_right = (LinearLayout) findViewById(R.id.search_right);
        RelativeLayout rl_left = (RelativeLayout) findViewById(R.id.rl_left);
        ImageView iv_add = (ImageView) findViewById(R.id.iv_add);
        if (search_right != null) {
            int l = maxwidth - (iv_add.getMeasuredHeight() + search_right.getMeasuredHeight());
            if (rl_left.getMeasuredWidth() > l) {
                LayoutParams layoutParams = (LayoutParams) rl_left.getLayoutParams();
                layoutParams.width = l - 40;
                rl_left.setLayoutParams(layoutParams);
            }
        }
    }
}
